package com.demo.crm.service;

import com.arvato.crm.repository.NamedQueryTools;
import com.demo.crm.model.AjaxInfo;

import org.apache.commons.collections.CollectionUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Collections;
import java.util.List;
import java.util.Map;

/**
 * @author  fzd
 */
@Service
public class TagTypeService {


    /**
     * @author fzd
     * 获取标签分组子级
     * @param id
     * @return
     */
    public List<Map<String, Object>> getChildren(String id){
        if (StringUtils.isEmpty(id)) id="0";

        List<Map<String,Object>> list= NamedQueryTools.queryForMap(
                NamedQueryTools.createDynamicSqlQuery("getTagTypeChild", null).setParameter("id", id)
        );

        if (CollectionUtils.isNotEmpty(list)){
            for(Map<String,Object> map:list){
                map.put("isParent",Integer.parseInt(map.get("childCount").toString())>0);
                map.remove("childCount");
            }
        }else{
            return Collections.EMPTY_LIST;
        }

        return list;
    }

    /**
     * @author fzd
     * 获取标签分组子级数量
     * @param id
     * @return
     */
    public int getChildrenCount(String id){
        return  Integer.parseInt(
            NamedQueryTools.queryForMap(
                    NamedQueryTools.createDynamicSqlQuery("getTagTypeChildCount", null).setParameter("id", id)
            ).get(0).get("childCount").toString()
        );
    }

    /**
     *
     * @param id
     * @return
     */
    public AjaxInfo canRemove(String id){
        //不可删除
        //  有子级
        //  被标签使用

        if (this.getChildrenCount(id)>0){
            return new AjaxInfo(false,"该分类下还有子级不能删除");
        }

        return new AjaxInfo(true);
    }
}
